package com.itheima.dao.impl;

import com.itheima.dao.UserDao;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class UserDaoImpl implements UserDao {
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override

    public void save() throws Exception {
        //1.获取数据库连接
        Connection connection = dataSource.getConnection();
        //2.sql语句
        String sql = "insert into userInfo values(id,?,?)";
        //3.获取预编译处理对象
        PreparedStatement pst = connection.prepareStatement(sql);
        //4.占位符传值
        pst.setString(1, "貂蝉");
        pst.setString(2, "666");
        //5.执行预编译处理
        int i = pst.executeUpdate();
        System.out.println(i);
        //6.提交事务

        //7.释放资源
        connection.close();
    }
}
